package com.service.impl;


import com.constant.AppConstant;
import com.dto.Page;
import com.dto.UserAndRoleDecn;
import com.dto.VersionDto;
import com.entity.Role;
import com.entity.SysVersionManage;
import com.entity.UserInfo;
import com.exception.ParamException;
import com.repository.SysVersionPagingAndSortingRepository;
import com.repository.SysVersionRepository;
import com.repository.UserPagingAndSortingRepository;
import com.service.PageService;
import com.utils.PageUtils;
import com.vo.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * @author Yadasu
 * @date 2020/12/15 9:00
 */
@Service
public class PageServiceImpl implements PageService {


    @Autowired
    private UserPagingAndSortingRepository userPagingAndSortingRepository;

    @Autowired
    private SysVersionRepository sysVersionRepository;
    @Autowired
    private SysVersionPagingAndSortingRepository repository;

    @Override
    public Page<UserAndRoleDecn> findAll(Pageable pageable) {

        List<UserInfo> all = (userPagingAndSortingRepository.findAll(pageable)).getContent();
        List<UserAndRoleDecn> list = new ArrayList<>();
        for (UserInfo userInfo : all) {
            UserAndRoleDecn userAndRoleDecn = new UserAndRoleDecn();
            userAndRoleDecn.setId(userInfo.getId());
            userAndRoleDecn.setNickname(userInfo.getNickname());
            userAndRoleDecn.setUsername(userInfo.getUsername());
            userAndRoleDecn.setEmail(userInfo.getEmail());
            userAndRoleDecn.setPhone(userInfo.getPhone());
            userAndRoleDecn.setEnabled(userInfo.isEnabled());
            List<Role> roles = userInfo.getRoles();
            for (Role role : roles) {
                userAndRoleDecn.setName_zh(role.getNameZh());
                list.add(userAndRoleDecn);
            }
        }
        Page<UserAndRoleDecn> page = new Page<>();
        page.setDatas(list);
        return page;
    }

    @Override
    public Page<UserAndRoleDecn> findAllByEnable(Pageable pageable, PageVo pageVo) {
        boolean flag;
        if (pageVo.getWords() == null && pageVo.getFlag() != null) {
            flag = pageVo.getFlag() == 1;
            List<UserInfo> all = (userPagingAndSortingRepository.findAllByEnabledTrue(flag, pageable)).getContent();
            return PageUtils.pages(all);
        } else if (pageVo.getWords() != null && pageVo.getFlag() == null) {
            List<UserInfo> all = (userPagingAndSortingRepository.findAllByNicknameContaining(pageVo.getWords(), pageable)).getContent();
            return PageUtils.pages(all);
        } else {
            flag = pageVo.getFlag() == 1;
            List<UserInfo> all = (userPagingAndSortingRepository.findAllByNicknameContainingAndEnabledTrue(pageVo.getWords(), flag, pageable)).getContent();
            return PageUtils.pages(all);
        }


    }

    @Override
    public Page<VersionDto> findAllVersion(Pageable pageable) {
        //TODO:需求将已部署未部署需要更新不需要更新的所有项目信息以dto的输出
        List<SysVersionManage> manages = (sysVersionRepository.findAll(pageable).getContent());
        List<VersionDto> versionDtos = new ArrayList<>();
        for (SysVersionManage sysVersionManage : manages) {
            if (sysVersionManage.getSysDeployHistory() != null) {
                Date deployDate = sysVersionManage.getSysDeployHistory().getDeployDate();
                Date updatedTime = sysVersionManage.getUpdatedTime();
                VersionDto versionDto = new VersionDto();
                if (sysVersionManage.getPackageVersion().equals(sysVersionManage.getCurrentVersion())) {
                    if (updatedTime != null && deployDate.before(updatedTime)) {
                        versionDto.setUpdate(!AppConstant.DEFAULT_FLAG);
                    } else {
                        versionDto.setUpdate(AppConstant.DEFAULT_FLAG);
                    }
                } else {
                    versionDto.setUpdate(!AppConstant.DEFAULT_FLAG);
                }
                versionDto.setAppName(sysVersionManage.getPackageName());
                versionDto.setDeployId(sysVersionManage.getDeployId());
                versionDto.setAppDeploymentTime(sysVersionManage.getSysDeployHistory().getDeployDate());
                versionDto.setAppVersion(sysVersionManage.getPackageVersion());
                versionDto.setAppCurrentVersion(sysVersionManage.getCurrentVersion());
                versionDto.setAppFileName(sysVersionManage.getFileName());
                versionDto.setAppUpdateTime(sysVersionManage.getUpdatedTime());
                versionDto.setAppRemoteDevelopment(!AppConstant.DEFAULT_FLAG);
                versionDto.setAppCreateTime(sysVersionManage.getCreatedTime());
                versionDto.setAppOwner(sysVersionManage.getCreateBy());
                versionDtos.add(versionDto);
            } else {
                VersionDto versionDto = new VersionDto();
                versionDto.setUpdate(AppConstant.DEFAULT_FLAG);
                versionDto.setAppName(sysVersionManage.getPackageName());
                versionDto.setDeployId(sysVersionManage.getDeployId());
                versionDto.setAppFileName(sysVersionManage.getFileName());
                versionDto.setAppVersion(sysVersionManage.getPackageVersion());
                versionDto.setAppCurrentVersion(sysVersionManage.getCurrentVersion());
                versionDto.setAppUpdateTime(sysVersionManage.getUpdatedTime());
                versionDto.setAppRemoteDevelopment(AppConstant.DEFAULT_FLAG);
                versionDto.setAppCreateTime(sysVersionManage.getCreatedTime());
                versionDto.setAppOwner(sysVersionManage.getCreateBy());
                versionDtos.add(versionDto);
            }
        }
        Page<VersionDto> page = new Page<>();
        page.setDatas(versionDtos);
        return page;
    }

    @Override
    public List<Page<VersionDto>> findAllVersionEnable(Pageable pageable, PageVo pageVo) {
        SysVersionManage sysVersionManage = new SysVersionManage();
        sysVersionManage.setPackageName(pageVo.getWords());
        ExampleMatcher exampleMatcher = ExampleMatcher.matching().withMatcher("packageName", ExampleMatcher.GenericPropertyMatcher::contains);
        Example<SysVersionManage> example = Example.of(sysVersionManage, exampleMatcher);
        org.springframework.data.domain.Page<SysVersionManage> all = repository.findAll(example, pageable);
        List<VersionDto> versionDtos = new ArrayList<>();
        if (StringUtils.isEmpty(all)) {
            throw new ParamException("数据不存在请查证后再查询！");
        } else {
            List<Page<VersionDto>> list = new ArrayList<>();


            for (int i = 0; i < all.getContent().size(); i++) {

                if (all.getContent().get(i).getSysDeployHistory() != null) {
                    Date deployDate = all.getContent().get(i).getSysDeployHistory().getDeployDate();
                    Date updatedTime = all.getContent().get(i).getUpdatedTime();
                    VersionDto versionDto = new VersionDto();
                    if (all.getContent().get(i).getPackageVersion().equals(all.getContent().get(i).getCurrentVersion())) {
                        if (updatedTime != null && deployDate.before(updatedTime)) {
                            versionDto.setUpdate(!AppConstant.DEFAULT_FLAG);
                        } else {
                            versionDto.setUpdate(AppConstant.DEFAULT_FLAG);
                        }
                    } else {
                        versionDto.setUpdate(!AppConstant.DEFAULT_FLAG);
                    }
                    versionDto.setAppName(all.getContent().get(i).getPackageName());
                    versionDto.setDeployId(all.getContent().get(i).getDeployId());
                    versionDto.setAppDeploymentTime(all.getContent().get(i).getSysDeployHistory().getDeployDate());
                    versionDto.setAppVersion(all.getContent().get(i).getPackageVersion());
                    versionDto.setAppFileName(sysVersionManage.getFileName());
                    versionDto.setAppCurrentVersion(all.getContent().get(i).getCurrentVersion());
                    versionDto.setAppUpdateTime(all.getContent().get(i).getUpdatedTime());
                    versionDto.setAppRemoteDevelopment(!AppConstant.DEFAULT_FLAG);
                    versionDto.setAppCreateTime(all.getContent().get(i).getCreatedTime());
                    versionDto.setAppOwner(all.getContent().get(i).getCreateBy());
                    versionDtos.add(versionDto);
                } else {
                    VersionDto versionDto = new VersionDto();
                    versionDto.setUpdate(AppConstant.DEFAULT_FLAG);
                    versionDto.setAppName(all.getContent().get(i).getPackageName());
                    versionDto.setDeployId(all.getContent().get(i).getDeployId());
                    versionDto.setAppFileName(sysVersionManage.getFileName());
                    versionDto.setAppVersion(all.getContent().get(i).getPackageVersion());
                    versionDto.setAppCurrentVersion(all.getContent().get(i).getCurrentVersion());
                    versionDto.setAppUpdateTime(all.getContent().get(i).getUpdatedTime());
                    versionDto.setAppRemoteDevelopment(AppConstant.DEFAULT_FLAG);
                    versionDto.setAppCreateTime(all.getContent().get(i).getCreatedTime());
                    versionDto.setAppOwner(all.getContent().get(i).getCreateBy());
                    versionDtos.add(versionDto);
                }

            }
            Page<VersionDto> page = new Page<>();
            page.setDatas(versionDtos);
            list.add(page);
            return list;
        }

    }
}
//        Optional<SysVersionManage> one = sysVersionRepository.findOne(example);
//        if (one.isPresent()) {
//            List<VersionDto> versionDtos = new ArrayList<>();
//            if (one.get().getSysDeployHistory() != null) {
//                Date deployDate = one.get().getSysDeployHistory().getDeployDate();
//                Date updatedTime = one.get().getUpdatedTime();
//                VersionDto versionDto = new VersionDto();
//                if (one.get().getPackageVersion().equals(one.get().getCurrentVersion())) {
//                    if (updatedTime != null && deployDate.before(updatedTime)) {
//                        versionDto.setUpdate(!AppConstant.DEFAULT_FLAG);
//                    } else {
//                        versionDto.setUpdate(AppConstant.DEFAULT_FLAG);
//                    }
//                } else {
//                    versionDto.setUpdate(!AppConstant.DEFAULT_FLAG);
//                }
//                versionDto.setAppName(one.get().getPackageName());
//                versionDto.setDeployId(one.get().getDeployId());
//                versionDto.setAppDeploymentTime(one.get().getSysDeployHistory().getDeployDate());
//                versionDto.setAppVersion(one.get().getPackageVersion());
//                versionDto.setAppCurrentVersion(one.get().getCurrentVersion());
//                versionDto.setAppUpdateTime(one.get().getUpdatedTime());
//                versionDto.setAppRemoteDevelopment(!AppConstant.DEFAULT_FLAG);
//                versionDto.setAppCreateTime(one.get().getCreatedTime());
//                versionDto.setAppOwner(one.get().getCreateBy());
//                versionDtos.add(versionDto);
//            } else {
//                VersionDto versionDto = new VersionDto();
//                versionDto.setUpdate(AppConstant.DEFAULT_FLAG);
//                versionDto.setAppName(one.get().getPackageName());
//                versionDto.setDeployId(one.get().getDeployId());
//                versionDto.setAppVersion(one.get().getPackageVersion());
//                versionDto.setAppCurrentVersion(one.get().getCurrentVersion());
//                versionDto.setAppUpdateTime(one.get().getUpdatedTime());
//                versionDto.setAppRemoteDevelopment(AppConstant.DEFAULT_FLAG);
//                versionDto.setAppCreateTime(one.get().getCreatedTime());
//                versionDto.setAppOwner(one.get().getCreateBy());
//                versionDtos.add(versionDto);
//            }
//            Page<VersionDto> page = new Page<>();
//            page.setDatas(versionDtos);
//            return page;
//        } else {
//            throw new ParamException("数据不存在请查证后再查询！");
//        }


